package com.lemon.smartsafetynew.ui.fragment.notice;

import android.os.Bundle;

import com.lemon.android.http.LemonRequestParams;
import com.lemon.smartsafety.app.RefreshType;
import com.lemon.smartsafety.listener.RecyclerTClickListener;
import com.lemon.smartsafetynew.ui.fragment.base.BasePullToRefreshFragment;
import com.lemon.smartsafety.utils.app.UIHelper;
import com.lemon.smartsafetynew.entity.event.common.ViewEvent;
import com.lemon.smartsafetynew.entity.notice.NoticeItemVo;
import com.lemon.smartsafetynew.net.services.NoticeServices;
import com.lemon.smartsafetynew.ui.activity.notice.NoticeDetailActivity;
import com.lemon.smartsafetynew.ui.adapter.notice.NoticeListNewAdapter;
import com.lemon.smartsafetynew.util.constants.KeyConstants;
import com.lemon.smartsafetynew.util.user.AccountUtil;
import com.zhy.http.okhttp.OkHttpUtils;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.List;

import kale.adapter.LemonRcvAdapter;

/**
 * Created by Andrew on 2017/11/3.
 */

public class NoticeListNewFragment extends BasePullToRefreshFragment<NoticeItemVo, List<NoticeItemVo>>
        implements RecyclerTClickListener<NoticeItemVo> {

    private String status = "";

    @Override
    protected void getBaseArguments() {
        super.getBaseArguments();

        Bundle bundle = getArguments();

        if (bundle != null) {
            if (bundle.containsKey(KeyConstants.KEY_NOTICE_KEY_STATUS)) {
                status = bundle.getString(KeyConstants.KEY_NOTICE_KEY_STATUS);
            }
        }
    }

    public static NoticeListNewFragment newInstance(String status) {

        Bundle args = new Bundle();
        args.putString(KeyConstants.KEY_NOTICE_KEY_STATUS, status);

        NoticeListNewFragment fragment = new NoticeListNewFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onPullRefresh() {

        LemonRequestParams params = new LemonRequestParams("");
        params.put("user_id", AccountUtil.getUserID());
        params.put("title", "");
        params.put("is_read", status);
        params.put("pageNum", currentPage);
        params.put("pageSize", pageSize);

        NoticeServices
                .init()
                .getNoticeList(RefreshType.REFRESH, params, status);

    }

    @Override
    public void onLoadMore() {


        LemonRequestParams params = new LemonRequestParams("");
        params.put("user_id", AccountUtil.getUserID());
        params.put("title", "");
        params.put("is_read", status);
        params.put("pageNum", currentPage);
        params.put("pageSize", pageSize);

        NoticeServices
                .init()
                .getNoticeList(RefreshType.LOAD_MORE, params, status);

    }

    @Override
    public boolean goRefreshOnCreate() {
        return true;
    }

    @Override
    public LemonRcvAdapter<NoticeItemVo> getLemonAdapter() {
        return new NoticeListNewAdapter(mDataList, this);
    }

    @Override
    public String getHttpTag() {
        return KeyConstants.URL_GET_NOTICE_LIST + status;
    }

    @Override
    public void onItemClick(NoticeItemVo data, int position) {

        Bundle bundle = new Bundle();
        bundle.putSerializable(KeyConstants.KEY_NOTICE_ITEM_VO, data);
        UIHelper.showActivity(getmContext(), bundle, NoticeDetailActivity.class);

    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onViewEvent(ViewEvent event) {
        if (NoticeDetailActivity.class.getName().equals(event.getTag())) {
            doPullRefresh();
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        OkHttpUtils.getInstance().cancelTag(KeyConstants.URL_GET_NOTICE_LIST + status);
    }
}
